﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;

public class mapSiteButton : MonoBehaviour, ICardboardGazeResponder
{
    public bool gazeEnter = false;
    float startPositionY;
    bool isJump = false;
    public int siteID;
    public string nextScene;

    // Use this for initialization
    void Start()
    {
        startPositionY = this.GetComponent<Transform>().position.y;
    }

    // Update is called once per frame
    void Update()
    {
        if (!gazeEnter)
            JumpIcon(startPositionY);
    }

    public void OnGazeEnter()
    {
        gazeEnter = true;
        this.GetComponentInChildren<Text>().text = this.transform.name;
        // Change the alpha of text
        Color textColor = new Color();
        textColor = this.GetComponentInChildren<Text>().color;
        textColor.a = 255;
        this.GetComponentInChildren<Text>().color = textColor;

    }

    public void OnGazeExit()
    {
        gazeEnter = false;

        // Change the alpha of text
        Color textColor = new Color();
        textColor = this.GetComponentInChildren<Text>().color;
        textColor.a = 0;
        this.GetComponentInChildren<Text>().color = textColor;
    }

    public void OnGazeTrigger()
    {
        NextScene();
    }

    // Go next to the map
    private static void TriggerOnMap()
    {
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 position = new Vector3();
        position = camera.GetComponent<Transform>().position;
        position.z++;
        if (position.z >= 3) position.z = 3;
        camera.GetComponent<Transform>().position = position;
    }

    private void NextScene()
    {
        SceneManager.LoadScene(nextScene);
    }

    // The icon can jump
    public void JumpIcon(float start)
    {
        Vector3 position = new Vector3();
        position = this.GetComponent<Transform>().position;
        position.y = start + 0.3f * Mathf.Sin(Time.time * 5);
        if (position.y >= start)
            this.GetComponent<Transform>().position = position;
    }
}
